source-extends-exclude.ts ➔ exclusion   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 25
rs 9.85
c 0
b 0
f 0
cc 2
1
import { ClassHash } from "../../src/main.types"
2
3
// type Excluder<
4
//   S extends Record<string, ClassHash>,
5
//   E extends {[K in keyof S]?: ClassHash}
6
// > = { [P in Exclude<keyof S, keyof E>]: S[P]; }
7
8
// interface Exclusion<
9
//   S extends Record<string, ClassHash>
10
// > {
11
//   (source: S, ex: {[K in keyof S]?: ClassHash}): Excluder<S, typeof ex>
12
// }
13
14
function exclusion<
15
  E extends Record<string, ClassHash>,
16
  S extends Record<keyof E, ClassHash>
17
>(
18
  source: S, ex: E
19
): { [P in Exclude<keyof S, keyof E>]: S[P]; }
20
{
21
  const $return = {...source}
22
  for (const k in ex) {
23
    delete $return[k]
24
  }
25
26
  return $return
27
}
28
29
const source: Record<"a"|"b"|"c"|"d"|"e", ClassHash> = {a: "a", b: undefined, c: "c", d: undefined, e: undefined}
30
31
const step0 = exclusion(
32
  //@ts-expect-error Property 'z' is missing in type
33
  source,
34
  {z: undefined}
35
)
36
, answ0: typeof step0 = {
37
  whatever: true
38
}
39
, step1 = exclusion(source, {a: "a", b: undefined})
40
, step2 = exclusion(step1, {"c": undefined})
41
//@ts-expect-error Property 'd' is missing
42
, answ
43
: typeof step2 = {
44
  e: "",
45
  //@ts-expect-error Object literal may only specify known properties, and 'z'
46
  z: ""
47
}
48
export {step2, answ0}